#include "entry.h"

namespace jolfs
{

Entry::Entry()
  : id_    (  0 ),
    parent_(  0 ),
    name_  ( "" ),
    size_  (  0 ),
    mode_  (  0 ),
    type_  (  0 ),
    status_(  0 ),
    atime_ (  0 ),
    mtime_ (  0 ),
    ctime_ (  0 )
{
}

Entry::Entry( const Entry& other )
  : id_    (             0 ),
    parent_( other.parent_ ),
    name_  ( other.name_   ),
    size_  ( other.size_   ),
    mode_  ( other.mode_   ),
    type_  ( other.type_   ),
    status_( other.status_ ),
    atime_ ( other.atime_  ),
    mtime_ ( other.mtime_  ),
    ctime_ ( other.ctime_  )
{
}

Entry::~Entry()
{
}

const Entry::id_t& Entry::getId() const
{
  return id_;
}

const Entry* Entry::getParent() const
{
  return parent_;
}

const Entry::name_t& Entry::getName() const
{
  return name_;
}

const Entry::size_t& Entry::getSize() const
{
  return size_;
}

const Entry::mode_t& Entry::getMode() const
{
  return mode_;
}

const Entry::type_t& Entry::getType() const
{
  return type_;
}

const Entry::status_t& Entry::getStatus() const
{
  return status_;
}

const Entry::timestamp_t& Entry::getAccessTime() const
{
  return atime_;
}

const Entry::timestamp_t& Entry::getModificationTime() const
{
  return mtime_;
}

const Entry::timestamp_t& Entry::getCreationTime() const
{
  return ctime_;
}

const EntryList& Entry::getChildren() const
{
  return children_;
}

EntryList& Entry::getChildren()
{
  return children_;
}

Entry* Entry::getChild( const std::string& name )
{
  EntryList::const_iterator it = children_.begin();
  for (; it!=children_.end(); ++it )
    if ( (*it)->getName() == name )
      return *it;
  return 0;
}

const Entry* Entry::getChild( const std::string& name ) const
{
  EntryList::const_iterator it = children_.begin();
  for (; it!=children_.end(); ++it )
    if ( (*it)->getName() == name )
      return *it;
  return 0;
}

void Entry::setId( const Entry::id_t& id )
{
  id_ = id;
}

void Entry::setParent( Entry* const parent )
{
  parent_ = parent;
}

void Entry::setName( const Entry::name_t& name )
{
  name_ = name;
}

void Entry::setSize( const Entry::size_t& size )
{
  size_ = size;
}

void Entry::setMode( const Entry::mode_t& mode )
{
  mode_ = mode;
}

void Entry::setType( const Entry::type_t& type )
{
  type_ = type;
}

void Entry::setStatus( const Entry::status_t& status )
{
  status_ = status;
}

void Entry::setAccessTime( const Entry::timestamp_t& atime )
{
  atime_ = atime;
}

void Entry::setModificationTime( const Entry::timestamp_t& mtime )
{
  mtime_ = mtime;
}

void Entry::setCreationTime( const Entry::timestamp_t& ctime )
{
  ctime_ = ctime;
}

void Entry::addChild( Entry* const child )
{
  EntryList::iterator it = std::find(children_.begin(), children_.end(), child );
  if ( it==children_.end() )
    children_.push_back( child );
  //TODO: lookup using name instead of pointer... and handle the 'else' case, cannot
  //add existing child.
}

void Entry::delChild( Entry* const child )
{
  EntryList::iterator it = std::find(children_.begin(), children_.end(), child );
  if ( it!=children_.end() )
    children_.erase( it );
  //TODO: handle the 'else' error case, cannot erase a non existing child
}

} // namespace jolfs